import { IndexedDBDataContext, EntityObjectIndexedDB } from "../common/@tiny/indexedDB/index";
import { tableDefine, dbVersion } from "./tableDefine";

export class ClerkDataContext extends IndexedDBDataContext {
    private inventory: Inventory;
    private category: Category;
    private unit: Unit;
    private modifier: Modifier;
    private cart: Cart;
    private soldOut: SoldOut;

    constructor() {
        super("clerkDB", dbVersion, tableDefine);

        this.inventory = new Inventory(this);
        this.category = new Category(this);
        this.unit = new Unit(this);
        this.modifier = new Modifier(this);
        this.cart = new Cart(this);
        this.soldOut = new SoldOut(this);
    }

    get Inventory() { return this.inventory; }
    get Category() { return this.category; }
    get Unit() { return this.unit; }
    get Modifier() { return this.modifier; }
    get Cart() { return this.cart; }
    get SoldOut() { return this.soldOut; }
}

export class Inventory extends EntityObjectIndexedDB<Inventory>{
    categories: Category[];
    //商品名称
    name: string;
    //图片
    pic: string;
    //商品分类
    categoryIds;
    //商品成本
    costPrice: number;
    price: number;
    //商品代码
    code: string;
    //规格
    model: string;
    //材质
    material: string;
    //单位
    unit: Unit;
    unitId: string;
    //颜色
    color: string;
    //产地
    madein: string;
    //描述
    description: string;
    //检索码
    searchCode: string;
    //上架下架
    onsale: boolean;
    //会员价格定义：json对象
    memberPrices: any;
    //商品选项
    modifiers: Modifier[];
    //套餐选项
    combo;
    //该商品总数
    Qty: number;
    toString() { return "Inventory"; }
}

export class Category extends EntityObjectIndexedDB<Category> {
    name: string;
    showOrder: number;
    parent: Category;
    store: any;
    toString() { return "Category"; }
}

export class Unit extends EntityObjectIndexedDB<Unit>{
    name: string;
    storeId: string;
    unitType?: string;
    toString() { return "Units"; }
}

export class Modifier extends EntityObjectIndexedDB<Modifier> {
    applyCategories: Category[];
    isGeneral: boolean;
    isRequired: boolean;
    name: string;
    singleton: boolean;
    storeId: string;
    options: any[];
    amount: number;
    toString() { return "Modifier"; }
}

export class Cart extends EntityObjectIndexedDB<Cart>{
    actualPrice: number;
    amount: number;
    discount: number;
    discounts: any[] = [];
    inventory: Inventory;
    inventoryCode: string;
    inventoryId: string;
    inventoryName: string;
    isEntireDiscount: boolean;
    modifiers: {
        amount: number;
        items: Modifier[];
    };
    price: number;
    qty: number;
    refundQty: number;
    showOrder: number;
    total: number;
    extended: CartExtended;
    costPrice?: number;
    toString() { return "Cart"; }
}

/**
 * 套餐组合
 */
export class ComboItem extends EntityObjectIndexedDB<ComboItem>{
    costPrice: number;
    extended;
    inventory: Inventory;
    inventoryCode: string;
    inventoryId: string;
    inventoryName: string;
    modifiers: Modifier[];
    price: number;
    plusPrice: number;
    qty: number;
    type: string;
    discount;
    discounts;
    status;
    amount;
    actualPrice;
    isEntireDiscount;
    memberPrices;
    refundQty;
    toString() { return "Combo"; }
}

interface CartExtended {
    categories?: Category[];
    combo?: any;
    description?: string;
    dishup?: DishUp;
    singleModifier?: number; //modifier价格
    oldAmount?: number; //不计算modifier的价格
    pickup?: string;//等叫叫起标识
}

interface DishUp {
    operateTime: Date,
    operator: { id: string, name: string },
    operatorId: string,
    qty: number,
    status: string//"all" : "part"
}

export class Order extends EntityObjectIndexedDB<Order> {
    amountDue: number = 0;
    cart: { items: Cart[], qty: number } = <any>{};
    cashier: { id: string, name: string };
    cashierId: string;
    checkout: any;
    checkoutMode: string;
    closeTime: number;
    createTime: number;
    creator: { id: string, name: string };
    creatorId: string;
    member;
    memberType;
    discount: number;
    discounts: Array<any>;
    description: string;
    headCount: number;
    lastPrintTime: number;
    orderDiscount: number;
    orderNo: string;
    orderType: string;
    paidAmount: number;
    payments: Payments[];
    printCount;
    refundAmount: number;
    refundCause: string;
    remainAmount: number = 0;
    refOrderId: { id: string, type: string }[];
    serialNo: string;
    smallChangeOff: number;
    status: string;
    storeId: string;
    subtotal: number;
    terminalName: string;
    total: number;
    sourceid: string;
    processStatus: ProcessStatus;
    extended: OrderExtended;
    master: string;
    preOrders: Order[];
    toString() { return "Orders"; }
}
interface OrderExtended {
    headCount?: number;
    table?: Table;
    tableParty?: TableParty;
    remind?: Remind;
}

interface Remind {
    remindTime: Date | string;
    operator: { id: string, name: string };
    remindCount: number;
}

interface Payments {
    id: string;
    amount: number;
    channel: string;    // 支付方式
    channelId: string;  // 支付类型id
    charge: number; // 找零
    createTime: number;
    payMode: string;    // 交易类型：pay 消费/ refund 退款
    received: number;   // 实收
    refunded: boolean;  // 是否已退
    storeId: string;    // 商铺Id
    extended: string;   // 扩展信息，用于包括支付所需记录的详细内容，或者其他内容。JSON格式
}

interface ProcessStatus {
    id: string;
    orderNo: string;
    statusType: string;
    status: string;
    detail: Detail[];
}

interface Detail {
    id: string;
    orderNo: string;
    statusType: string;
    status: string;
    createTime: number;
}

export class Table extends EntityObjectIndexedDB<Table>{
    // 台桌名
    name: string;
    //座位数
    capacity: number;
    //分区
    zone: any;
    //店铺id
    storeId: string | number;
    //排序字段
    showOrder;
    //当前台桌状态 free空闲 / opening 使用中 / reserved 预留 / settled 已结算未关台
    status: string;
    creator: { id: string; name: string };
    masterOrder: any;
    tableParty: any;
    toString(): string { return "DeskTable"; }
}
export class TableZone extends EntityObjectIndexedDB<TableZone>{
    //台桌分区
    name: string;
    //分区样式定义
    style;
    //店铺id
    storeId: string | number;
    toString(): string { return "TableZone"; }
    clone(source) {
        let r = super.clone(source, new TableZone(), false);
        delete (r as any).tables;
        return r;
    }
}
export class TableGroup extends EntityObjectIndexedDB<TableGroup> {
    //店铺id
    storeId: string | number;
    toString(): string { return "TableGroup"; }
}
/**
 * TableParty 台桌服务记录
 */
export class TableParty extends EntityObjectIndexedDB<TableParty>{
    //店铺id
    storeId: string | number;
    //台桌ID
    tableId: string | number;
    tableName: string;
    //加入合并台桌id。null表示独立台桌
    tableGroupId: string | number;
    //开台时间
    openedTime: Date | string;
    //关台时间
    closedTime: Date | string;
    // opening 使用中 closed 台桌已经关闭停止服务  settled: 已经结算状态
    status: string;
    //订单号
    orderId: string | number;
    //是否有效台桌使用。例如未就餐的台桌服务为无效台桌服务。开始默认为 true
    valid: boolean = true;
    toString(): string { return "TableParty"; }
}

export class DataContextManager {
    static Current: DataContextManager = new DataContextManager();
    private clerkCtx: ClerkDataContext
    constructor() { }

    get ClerkCtx() { return this.clerkCtx; }

    Regist() {
        this.clerkCtx = new ClerkDataContext();
    }
}

export interface SyncDataUrl {
    updateId;
    id;
    state;
    type: string;
    url: string;
    complete: boolean;
    failed: boolean;
}

export interface Terminal {
    id: string;
    name: string;
    bound: boolean;
    boundApp: string;
    hardware: string;
    deviceType: string;
    deviceId: string;
    platform: string;
    platformVersion: string;
    userAgent: string;
    os: string;
    store: any;
}

export class SerialNoEntity {
    domain: number;
    sernialNo: number;
    domainType: string;
}

export class SoldOut extends EntityObjectIndexedDB<SoldOut>{
    operTime: number;
    inventoryId: string;
    disabled: boolean;
    toString() {
        return "SoldOut";
    };
}

export class Remember {
    isRemember: boolean;
    username: string;
    password: string;
}